﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LoginForm.cs" company="HelloWorld">
//   BY田新宇
// </copyright>
// <summary>
//   登录窗口
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Form
{
    using System;
    using System.Windows.Forms;

    using WindowsFormsApp2.Model;
    using WindowsFormsApp2.Service;

    /// <summary>
    /// The login form.
    /// </summary>
    public partial class LoginForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LoginForm"/> class.
        /// </summary>
        public LoginForm()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the student id.
        /// </summary>
        public static string StudentId { get; set; }

        /// <summary>
        /// The main_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void 主界面_Load(object sender, EventArgs e)
        {
            this.label5.Text = string.Empty;
            this.MaximizeBox = false; // 关闭登录窗口的最大化功能
            this.Verification_Code.Text = VerificationCode.Code(); // 动态生成验证码
            this.button1.Enabled = false; // 默认在不输入验证码时无法点击登录按钮
        }

        /// <summary>
        /// The button 2_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button2_Click(object sender, EventArgs e)
        {
            // 注册
            var zc = new RegisterForm();
            zc.Show();
        }

        /// <summary>
        /// The button 1_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button1_Click(object sender, EventArgs e)
        {
            if (this.txtxuehao.Text.Trim() == string.Empty)
            {
                MessageBox.Show(@"用户名不能为空");
                this.txtxuehao.Focus();
                return;
            }
            else if (this.txtmima.Text.Trim() == string.Empty)
            {
                MessageBox.Show(@"密码不能为空!");
                this.txtmima.Focus();
                return;
            }

            if (this.txtvericode.Text != this.Verification_Code.Text)
            {
                this.label5.Text = string.Empty;
                return;
            }

            var db = new LinkSqLiteDB(); // 创建数据库连接对象
            if (db.DBConn())
            {
                // 数据库连接成功
                if (db.GetLoginData("select * from login_info where stuid='" + this.txtxuehao.Text + "'"))
                {
                    // 用户存在
                    if (true)
                    {
                        // 账号已被审核允许登录
                        if (this.txtmima.Text.Equals(LoginInfo.Password))
                        {
                            // 登录成功
                            MessageBox.Show(@"恭喜您已成功登录", @"确定", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                            // 主菜单 zhucaidan = new 主菜单();
                            // zhucaidan.ShowDialog();
                            StudentId = txtxuehao.Text;
                            db.DBClose();
                            this.DialogResult = DialogResult.OK;
                            return;
                        }

                        // 密码错误
                        MessageBox.Show(@"登录失败,请重新输入");
                        this.txtxuehao.Text = string.Empty;
                        this.txtmima.Text = string.Empty;
                        return;
                    }
                }

                MessageBox.Show(@"登录失败，用户名不存在!");
                return;
            }

            db.DBClose(); // 关闭数据库连接
        }

        /// <summary>
        /// The TxtVerifyCode_ key up.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void TxtVerifyCode_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.txtvericode.Text != this.Verification_Code.Text)
            {
                this.label5.Text = string.Empty;
            }
            else
            {
                this.label5.Text = string.Empty;
                this.button1.Enabled = true;
            }
        }

        // 验证用户登录
    }
}
